package mipo.top.admin.service.impl;

import mipo.top.admin.Dto.EmpDto;
import mipo.top.admin.domain.Employee;
import mipo.top.users.domain.Logininfo;
import mipo.top.admin.domain.Shop;
import mipo.top.admin.domain.ShopAuditLog;
import mipo.top.admin.mapper.EmployeeMapper;
import mipo.top.users.mapper.LogininfoMapper;
import mipo.top.admin.mapper.ShopAuditLogMapper;
import mipo.top.admin.mapper.ShopMapper;
import mipo.top.admin.query.ShopQuery;
import mipo.top.admin.service.IShopService;
import mipo.top.base.exception.BusinessException;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.base.util.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ShopServiceimpl extends BaseServiceimpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public PageUtil<Shop> queryData(ShopQuery shopQuery) {
       /* Integer count = shopMapper.count(shopQuery);
        List<Shop> shops = shopMapper.pageQuery(shopQuery);
        for (Shop shop : shops) {
            Long shopId = shop.getId();
            List<ShopAuditLog> shopAuditLog = shopAuditLogMapper.findByShopId(shopId);
            shop.setShopAuditLog(shopAuditLog);
        }
        return new PageUtil<Shop>(count,shops);*/
        PageUtil<Shop> shopPageUtil = super.queryData(shopQuery);
        List<Shop> rows = shopPageUtil.getRows();
        List<Long> list = rows.stream().map(Shop::getId).collect(Collectors.toList());
        List<ShopAuditLog> auditLog = shopAuditLogMapper.findByIds(list);
        Map<Long, List<ShopAuditLog>> log = auditLog.stream().collect(Collectors.groupingBy(ShopAuditLog::getShopId));
        rows.forEach(row->{
            row.setShopAuditLog(log.get(row.getId()));
        });
        return shopPageUtil;
    }

    //校验店铺入住数据
    @Override
    @Transactional
    public void joinShop(Shop shop) {
        String shopName = shop.getName();
        EmpDto admin = shop.getAdmin();
        if (StringUtils.isEmpty(shopName)){
            throw new BusinessException("店铺名称不能为空");
        }
        Employee s = shopMapper.findname(shopName);
        if (s!=null){
            throw new BusinessException("店铺名称已经存在");
        }
        if(!BaiduAuditUtils.TextCensor(shopName)||!BaiduAuditUtils.TextCensor(admin.getUsername())){
            throw new BusinessException("不要讲藏话");
        }
        String username = admin.getUsername();
        if (StringUtils.isEmpty(admin.getUsername())){
            throw new BusinessException("绑定的管理者不能为空");
        }
        Employee employee = employeeMapper.findusername(username);
        if(employee!=null){
            throw new BusinessException("用户名已经存在");
        }
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次输入的密码不一致");
        }
        addShopandEmp(shop, admin);
    }

    @Override
    public void handpass(ShopAuditLog shopAuditLog) throws Exception {
        if (shopAuditLog.getNote()==null){
            throw new BusinessException("备注不能为空");
        }
        if(shopAuditLog.getShopId()==null){
            throw new BusinessException("店铺不能为空");
        }
        Shop shop = shopMapper.findone(shopAuditLog.getShopId());
        shop.setState(1);
        //Long adminId = shop.getAdminId();
        //Employee findone = employeeMapper.findone(adminId);
        //方便验证就不用emp表的假数据来填入邮箱
        SimpleMailMsg.pass("2430618938@qq.com",shop);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(356L);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.save(shopAuditLog);
    }
    @Override
    public void handreject(ShopAuditLog shopAuditLog) throws Exception {
        if (shopAuditLog.getNote()==null){
            throw new BusinessException("备注不能为空");
        }
        if(shopAuditLog.getShopId()==null){
            throw new BusinessException("店铺不能为空");
        }
        Shop shop = shopMapper.findone(shopAuditLog.getShopId());
        shop.setState(-1);
        //Long adminId = shop.getAdminId();
        //Employee findone = employeeMapper.findone(adminId);
        //方便验证就不用emp表的假数据来填入邮箱
        SimpleMailMsg.pass("2430618938@qq.com",shop);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(356L);
        shopAuditLog.setAuditTime(new Date());
        shopAuditLogMapper.save(shopAuditLog);
    }
    //店铺入住数据录入
    private void addShopandEmp(Shop shop, EmpDto admin) {
        shopMapper.save(shop);

        Employee employees = getEmployee(shop, admin);
        //将数据
        Logininfo logininfo = getLogininfo(employees);
        logininfoMapper.save(logininfo);

        employees.setLogininfoId(logininfo.getId());
        employeeMapper.save(employees);

        shop.setAdminId(employees.getId());
        shop.setRegisterTime(new Date());
        shopMapper.update(shop);
    }

    @NotNull
    private Employee getEmployee(Shop shop, EmpDto admin) {
        String s = StrUtils.getComplexRandomString(32);
        String md5password = MD5Utils.encrypByMd5(s + admin.getPassword());
        Employee employees = new Employee();
        employees.setState(1);
        employees.setShopId(shop.getId());
        employees.setPhone(admin.getPhone());
        employees.setUsername(admin.getUsername());
        employees.setSalt(s);
        employees.setPassword(md5password);
        employees.setEmail(admin.getEmail());
        return employees;
    }

    @NotNull
    private Logininfo getLogininfo(Employee employees) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employees, logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    public void remove(Long id) {
        Shop findone = shopMapper.findone(id);
        Long adminId = findone.getAdminId();
        if(adminId!=null) {
            Employee employee = employeeMapper.findone(adminId);
            employeeMapper.remove(adminId);
            if(employee.getLogininfoId()!=null) {
                logininfoMapper.remove(employee.getLogininfoId());
            }
        }
        super.remove(id);
    }

}
